package com.zhongli.devplatform.service.impl.sys;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhongli.devplatform.bean.PageVO;
import com.zhongli.devplatform.dto.sys.DictionaryDirectoryDTO;
import com.zhongli.devplatform.exception.ServiceException;
import com.zhongli.devplatform.mapper.sys.DictionaryDirectoryMapper;
import com.zhongli.devplatform.mapper.sys.DictionaryItemMapper;
import com.zhongli.devplatform.po.sys.DictionaryDirectoryPO;
import com.zhongli.devplatform.po.sys.DictionaryItemPO;
import com.zhongli.devplatform.query.sys.DictionaryDirectoryQuery;
import com.zhongli.devplatform.service.imp.sys.IDictionaryDirectoryService;
import com.zhongli.devplatform.utils.BeanCopierUtil;
import com.zhongli.devplatform.vo.sys.DictionaryDirectoryVO;
import com.zhongli.devplatform.vo.sys.DictionaryItemVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DictionaryDirectoryServiceImpl implements IDictionaryDirectoryService {

    @Autowired
    private CacheManager cacheManager;

    @Autowired
    private DictionaryDirectoryMapper dictionaryDirectoryMapper;

    @Autowired
    private DictionaryItemMapper dictionaryItemMapper;

    @Value("${dev-platform.cache-name}")
    private String cacheName;


    @Override
    public PageVO<DictionaryDirectoryVO> listPage(DictionaryDirectoryQuery query) {

        QueryWrapper queryWrapper = new QueryWrapper<DictionaryDirectoryPO>();
        if (!StringUtils.isBlank(query.getName())) {
            queryWrapper.eq("code", query.getName());
            queryWrapper.or(true).like("name", query.getName());
        }
        if (!StringUtils.isBlank(query.getType())) {
            queryWrapper.eq("type", query.getType());
        }
        Page page = dictionaryDirectoryMapper.selectPage(query, queryWrapper);
        PageVO newPage = PageVO.convert(page);
        List<DictionaryDirectoryVO> dictionaryDirectoryVOList = BeanCopierUtil.copy(page.getRecords(), DictionaryDirectoryVO.class);
        newPage.setRecords(dictionaryDirectoryVOList);
        return newPage;
    }

    @Override
    @PostConstruct
    public void putCache() {
        List<DictionaryDirectoryPO> dictionaryDirectoryPOList = dictionaryDirectoryMapper.selectList(null);
        for (DictionaryDirectoryPO dictionaryDirectoryPO : dictionaryDirectoryPOList) {

            List<DictionaryItemPO> dictionaryItemPOList = dictionaryItemMapper.selectList(
                    new QueryWrapper<DictionaryItemPO>().eq("dictionary_directory_id", dictionaryDirectoryPO.getId())
                            .orderByDesc("order_num"));
            List<DictionaryItemVO> dictionaryItemVOList = BeanCopierUtil.copy(dictionaryItemPOList, DictionaryItemVO.class);
            cacheManager.getCache(cacheName + CACHE_KEY).put(dictionaryDirectoryPO.getCode(), dictionaryItemVOList);
        }
    }

    @Override
    public void save(DictionaryDirectoryDTO dictionaryDirectoryDTO) {
        DictionaryDirectoryPO dictionaryDirectoryPO = BeanCopierUtil.copy(dictionaryDirectoryDTO, DictionaryDirectoryPO.class);
        if (dictionaryDirectoryPO.getId() == null) {
            dictionaryDirectoryMapper.insert(dictionaryDirectoryPO);
        } else {
            dictionaryDirectoryMapper.updateById(dictionaryDirectoryPO);
        }

    }

    @Override
    public void delete(List<Long> ids) {
        dictionaryDirectoryMapper.deleteBatchIds(ids);
    }

    @Override
    public Boolean isExist(DictionaryDirectoryQuery query) {
        QueryWrapper queryWrapper = new QueryWrapper<DictionaryDirectoryPO>();
        if (query.getId() != null) {
            queryWrapper.ne("id", query.getId());
        }
        queryWrapper.eq("code", query.getCode());
        return dictionaryDirectoryMapper.selectCount(queryWrapper) > 0;
    }

    @Override
    public String getName(String code, String value) {
        List<DictionaryItemVO> dictionaryItemVOList = cacheManager.getCache(cacheName + CACHE_KEY).get(code, ArrayList::new);
        List<DictionaryItemVO> results = dictionaryItemVOList.stream().filter(item -> item.getValue().equals(value)).collect(Collectors.toList());
        if (results.isEmpty()) {
            throw new ServiceException("字典编码【" + code + "】不存在[" + value + "]字典键值");
        }
        return results.get(0).getName();
    }

    @Override
    public String getValue(String code, String name) {
        List<DictionaryItemVO> dictionaryItemVOList = cacheManager.getCache(cacheName + CACHE_KEY).get(code, ArrayList::new);
        List<DictionaryItemVO> results = dictionaryItemVOList.stream().filter(item -> item.getName().equals(name)).collect(Collectors.toList());
        if (results.isEmpty()) {
            throw new ServiceException("字典编码【" + code + "】不存在[" + name + "]字典名称");
        }
        return results.get(0).getValue();
    }

    @Override
    public List<DictionaryItemVO> getItems(String code) {

        List<DictionaryItemVO> dictionaryItemVOList = (List<DictionaryItemVO>) cacheManager.getCache(cacheName + IDictionaryDirectoryService.CACHE_KEY).get(code).get();

        return dictionaryItemVOList;
    }
}
